﻿using DeviceManager.ContentPage.SysMng;
using DeviceManager.DB;
using DeviceManager.DB.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DeviceManager.ContentPage
{
    /// <summary>
    /// DeviceManagerPage.xaml 的交互逻辑
    /// </summary>
    public partial class DeviceManagerPage : Page
    {
        List<DeviceNode> deviceNodes = null;
        public DeviceManagerPage()
        {
            InitializeComponent();
            this.Loaded += DeviceManagerPage_Loaded;
        }

        private void DeviceManagerPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                RefData();
            }
        }

        private void RefData()
        {
            //这里应该可以组建查询
            deviceNodes = ManagerDBCtrl.GetMng().GetAllDevices("");
            lv_devices.ItemsSource = deviceNodes;
        }


        private void blt_newDeviceInfo_Click(object sender, RoutedEventArgs e)
        {
            EditDeviceInfo editDeviceInfo = new EditDeviceInfo();
            if (editDeviceInfo.ShowDialog() == true)
            {
                RefData();
            }
        }

        private void blt_exportDeviceInfo_Click(object sender, RoutedEventArgs e)
        {
            ExportDeviceInfo exportDeviceInfo = new ExportDeviceInfo();
            if (exportDeviceInfo.ShowDialog() == true)
            {
                RefData();
            }
        }

        private void blt_del_Click(object sender, RoutedEventArgs e)
        {
            Button blt = sender as Button;
            DeviceNode user = blt.DataContext as DeviceNode;
            if (user != null)
            {
                if (ManagerDBCtrl.GetMng().DelDeviceNode(user.Id))
                {
                    RefData();
                }
            }
        }

        private void blt_edit_Click(object sender, RoutedEventArgs e)
        {
            Button blt = sender as Button;
            DeviceNode deviceNode = blt.DataContext as DeviceNode;
            if (deviceNode != null)
            {
                EditDeviceInfo editDeviceInfo = new EditDeviceInfo();
                editDeviceInfo.SetData(deviceNode);
                if (editDeviceInfo.ShowDialog() == true)
                {
                    RefData();
                }
            }
        }
    }
}
